﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kassasysteem_TacoTruijens
{
    public partial class confirmtickets : System.Web.UI.Page
    {
        private DataClassesDataContext Database = new DataClassesDataContext();
        private Reservering reservation;
        private List<Ticket> tickets = new List<Ticket>();

        protected void Page_Load(object sender, EventArgs e)
        {
            initTablePriceOverview();
        }

        private void initTablePriceOverview()
        {
            int reservationNumber = Convert.ToInt32(Request.QueryString["reservationnumber"]);

            var reservations = from r in Database.Reserverings
                               where r.nummer == reservationNumber
                               select r;

            reservation = reservations.First();

            var ticketsVar = from t in Database.Tickets
                             where t.Reservering.Equals(reservation)
                             select t;

            int adults = 0;
            int children = 0;
            int fiftyPlus = 0;
            int cjpStudents = 0;
            int free = 0;
            int drieDGlasses = 0;     

            foreach (Ticket t in ticketsVar)
            {
                switch (t.tickettype_naam)
                {
                    case "volwassen":
                        adults += 1;
                        break;
                    case "kind":
                        children += 1;
                        break;
                    case "50+":
                        fiftyPlus += 1;
                        break;
                    case "CJP/student":
                        cjpStudents += 1;
                        break;
                    case "gratis":
                        free += 1;
                        break;
                    case "3D Bril":
                        drieDGlasses += 1;
                        break;
                }

                tickets.Add(t);
            }

            DateTime dateTime = tickets.First().voorstelling_datumtijd;
            int roomNumber = tickets.First().zaal_nummer;
            String movieName = tickets.First().film_naam;

            labelMoviename.Text = movieName;
            labelDatetime.Text = dateTime.ToString();
            labelRoom.Text = roomNumber.ToString();
            labelAdultCount.Text = adults.ToString();
            labelChildCount.Text = children.ToString();
            labelFiftyPlusCount.Text = fiftyPlus.ToString();
            labelCJPStudentCount.Text = cjpStudents.ToString();
            labelFreeCount.Text = free.ToString();
            labelDrieDGlassesCount.Text = drieDGlasses.ToString();

            decimal adultPrice = adults * getPrice("volwassen");
            decimal childPrice = children * getPrice("kind");
            decimal fiftyPlusPrice = fiftyPlus * getPrice("50+");
            decimal cjpStudentPrice = cjpStudents * getPrice("CJP/student");
            decimal freePrice = free * getPrice("gratis");
            decimal drieDGlassesPrice = drieDGlasses * getPrice("3D Bril");

            labelAdultPrice.Text = "EUR " + adultPrice.ToString("#0.#####");
            labelChildPrice.Text = "EUR " + childPrice.ToString("#0.#####");
            labelFiftyPlusPrice.Text = "EUR " + fiftyPlusPrice.ToString("#0.#####");
            labelCJPStudentPrice.Text = "EUR " + cjpStudentPrice.ToString("#0.#####");
            labelFreePrice.Text = "EUR " + freePrice.ToString("#0.#####");
            labelDrieDGlassesPrice.Text = "EUR " + drieDGlassesPrice.ToString("#0.#####");
            labelTotalPrice.Text = "EUR " + (adultPrice + childPrice + fiftyPlusPrice + cjpStudentPrice + freePrice + drieDGlassesPrice).ToString("#0.#####");
        }

        private decimal getPrice(String type)
        {
            var prices = from t in Database.Tickettypes
                         where t.naam.Equals(type)
                         select t;

            return prices.First().prijs;
        }

        private void confirmReservation()
        {
            foreach (Ticket t in tickets)
            {
                t.Reservering = null;
                t.status_naam = "betaald";
            }

            Database.SubmitChanges();
        }

        protected void buttonNext_Click(object sender, EventArgs e)
        {
            confirmReservation();

            String queryString = "printtickets.aspx?moviename=" + tickets.First().Voorstelling.Film.naam + "&tickets=" + tickets.First().nummer + ":" + tickets.First().Tickettype.naam + ":" + tickets.First().Stoel.nummer + ":" + tickets.First().Stoel.Rij.nummer;

            for (int i = 1; i < tickets.Count; i++)
            {
                Ticket t = tickets[i];
                queryString += ";" + t.nummer + ":" + t.Tickettype.naam + ":" + t.Stoel.nummer + ":" + t.Stoel.Rij.nummer;
            }

            Response.Redirect(queryString);
        }
    }
}